var gulp = require("gulp");
var imagemin = require("gulp-imagemin");
var uglify = require("gulp-uglify");
var concat = require("gulp-concat");
var connect = require("gulp-connect");
var less = require("gulp-less");
var postcss = require("gulp-postcss");
var strip = require("gulp-strip-debug");
var autoprefixer = require("autoprefixer");
var cssnano = require("cssnano");
var htmlclean = require("gulp-htmlclean");
var sourceMap = require("gulp-sourcemaps");

var devMode = process.env.Node_ENV === "development";

var folder = {
    src: "./src",
    dist: "./dist"
};

gulp.task("images", function () {
    gulp.src(folder.src + "/images/*")
        .pipe(imagemin())
        .pipe(gulp.dest(folder.dist + "/images"))
        .pipe(connect.reload());
});


gulp.task("js", function () {
    gulp.src(folder.src + "/js/*.js")
        .pipe(sourceMap.init())
        // .pipe(uglify())
        .pipe(sourceMap.write())
        .pipe(gulp.dest(folder.dist + "/js"))
        .pipe(connect.reload());
});

gulp.task("html", function () {
    gulp.src(folder.src + "/*.html")
        .pipe(htmlclean())
        .pipe(gulp.dest(folder.dist))
        .pipe(connect.reload());
});

gulp.task("style", function () {
    gulp.src(folder.src + "/style/*.less")
        .pipe(less())
        .pipe(postcss(
            [autoprefixer(), cssnano()]
        ))
        .pipe(gulp.dest(folder.dist + "/css"))
        .pipe(connect.reload());
});

gulp.task("server", function () {
    connect.server({
        name: "Dev App",
        root: ["dist"],
        port: 8000,
        livereload: true
    });
});

gulp.task("watch", function () {
    gulp.watch(folder.src + "/*", ["html"]);
    gulp.watch(folder.src + "/js/*", ["js"]);
    gulp.watch(folder.src + "/images/*");
    gulp.watch(folder.src + "/style/*", ["style"]);
});

gulp.task("default", ["images", "html", "style", "js", "watch", "server"]);